﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Data;

namespace Ewts.ashx
{
    /// <summary>
    /// Autocomplete 的摘要说明
    /// </summary>
    public class Autocomplete : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            StringBuilder items = new StringBuilder();
            DataTable dt = new DataTable();
            string strwhere = "";
            DataLib.Model.Goods model = new DataLib.Model.Goods();
            //if (context.Request["goods"] != null && context.Request["goods"].ToString().Trim() != "")
            //{
            //    strwhere = "goodsName like '%" + context.Request["goods"].ToString().Trim() + "%'";
            //}

            if (context.Request["type"] != null && context.Request["type"].ToString().Trim() != "")
            {
                string strtype = context.Request["type"].ToString().Trim();

                if (strtype == "hw")
                {
                    if (context.Request["goods"] != null && context.Request["goods"].ToString().Trim() != "")
                    {
                        strwhere = "itemCode like '%" + context.Request["goods"].ToString().Trim() + "%'";
                    }
                    //strwhere = "itemCode";
                    dt = new DataLib.BLL.Rubbish().GetList(10,strwhere,"id").Tables[0];
                    if (dt != null && dt.Rows.Count > 0)
                    {
                        items.Append("[");
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            items.Append("{\"value\":\"");
                            items.Append(dt.Rows[i]["itemName"].ToString());
                            items.Append("\",\"label\":\"");
                            items.Append(dt.Rows[i]["itemCode"].ToString());
                            items.Append("\",\"rubid\":\"");
                            items.Append(dt.Rows[i]["id"].ToString());
                            items.Append("\"},");
                         
                        }
                        items.Remove(items.Length - 1, 1);
                        items.Append("]");

                    }

                }
                else
                {
                    if (context.Request["goods"] != null && context.Request["goods"].ToString().Trim() != "")
                    {
                        strwhere = "goodsName like '%" + context.Request["goods"].ToString().Trim() + "%' ";
                    }


                    if (strtype == "DH")
                    {
                        strwhere += "and (goodsType = '临时库' or goodsType ='废弃物') ";
                    }
                    else if (strtype == "D")
                    {
                        strwhere += "and goodsType = '临时库'";
                    }
                    else if (strtype == "B")
                    {
                        strwhere += "and (goodsType = '成品' or goodsType='半成品') ";
                    }
                    else if (strtype == "T")
                    {
                        strwhere += "and goodsType = '原料'";
                    }
                    else if (strtype == "BDH")
                    {
                        strwhere += "and (goodsType = '成品' or goodsType='半成品' or goodsType='临时库' or goodsType='废弃物')";
                    }
                    dt = new DataLib.BLL.Goods().GetList(10,strwhere,"id").Tables[0];
                    items.Append("[");
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        items.Append("{\"value\":\"");
                        items.Append(dt.Rows[i]["describe"].ToString());
                        items.Append("\",\"label\":\"");
                        items.Append(dt.Rows[i]["goodsName"].ToString());
                        items.Append("\",\"goodsid\":\"");
                        items.Append(dt.Rows[i]["id"].ToString());
                        items.Append("\"},");

                    }
                    items.Remove(items.Length - 1, 1);
                    items.Append("]");
                }




            }



            context.Response.Write(items);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}